package com.tsinghua.server.original.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.tsinghua.server.common.utils.ExcleUtil;
import com.tsinghua.server.original.entity.FxjcCtz;
import com.tsinghua.server.original.entity.FxjcCyz;
import com.tsinghua.server.original.entity.FxjcJrz;
import com.tsinghua.server.original.entity.FxjqCyzEntity;
import com.tsinghua.server.original.service.DataSetService;
import com.tsinghua.server.original.service.FxjcCtzService;
import com.tsinghua.server.original.service.FxjcCyzService;
import com.tsinghua.server.original.service.FxjcJrzService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author Mr.Eric
 * @description:
 * @date 2022/3/2 13:40
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class DataSetServiceImpl implements DataSetService {

    private final FxjcCtzService fxjcCtzService;

    private final FxjcJrzService fxjcJrzService;

    private final FxjcCyzService fxjcCyzService;


    @Override
    public boolean importFile(MultipartFile file) {
        long start = System.currentTimeMillis();
        List<FxjcCyz> fxjqCyzEntities = ExcleUtil.importData(file,1,FxjcCyz.class);
        log.info("导入文件:{}", JSONObject.toJSONString(fxjqCyzEntities));
        if (CollUtil.isNotEmpty(fxjqCyzEntities)){
            fxjcCyzService.saveBatch(fxjqCyzEntities);
        }
        long end = System.currentTimeMillis();
        log.info("数据导入时间："+(end-start)+"ms");
        return true;
    }

    @Override
    public boolean importCTZFile(MultipartFile file) {
        List<FxjcCtz> fxjcCtzs = ExcleUtil.importData(file,1,FxjcCtz.class);
        log.info("导入文件:{}", JSONObject.toJSONString(fxjcCtzs));
        if (CollUtil.isNotEmpty(fxjcCtzs)){
            fxjcCtzService.saveBatch(fxjcCtzs);
        }
        return true;
    }

    @Override
    public boolean importJRZFile(MultipartFile file) {
        List<FxjcJrz> fxjcJrzs = ExcleUtil.importData(file,1,FxjcJrz.class);
        log.info("导入文件:{}", JSONObject.toJSONString(fxjcJrzs));
        if (CollUtil.isNotEmpty(fxjcJrzs)){
            fxjcJrzService.saveBatch(fxjcJrzs);
        }
        return true;
    }
}
